Ext.define('Usws.controller.VideoFile', {
    extend: 'Ext.app.Controller',
    stores:['VideoFile'],
    
    refs: [
   		   {
               ref: 'contentPanel',
               selector: 'contentPanel'
           }
    ],

    init: function() {
        this.control({
            'VideoFile-Grid': {
                itemdblclick: this.editVideoFile,
                add: this.loadVideoFile
            },
            'VideoFile-Grid button[action=add]': {
                click: this.addVideoFile
            },
            
            'VideoFile-Grid button[action=deleteMore]':{  
                click: this.deleteVideoFileMore
            },
            
            'VideoFile-Edit button[action=save]': {
                click: this.updateVideoFile
            },
            'VideoFile-Grid actioncolumn':{
            	itemclick: this.actionColumnOptVideoFile
             },
             
             '#uploadVideoFilePreviewPic': {
            	 change: this.uploadVideoFilePreviewPic
             },
             
             '#uploadVideoFile': {
            	 change: this.uploadVideoFile
             },
        });
    },
    
    loadVideoFile:function(obj, component, index, eOpts){
    	var videoFileStore = this.getVideoFileStore();
    	videoFileStore.load();
    },
    
    addVideoFile: function(button) {
   	    var view = Ext.widget('VideoFile-Edit');
   },

    editVideoFile: function(grid, record) {
    	 var view = Ext.widget('VideoFile-Edit');
    	 
    	 var videoSnapshotURL = record.get("videoSnapshotURL");
 	     if(videoSnapshotURL) {
 	    	 Ext.getCmp('previewVideoFilePreviewPic').body.update('<img src="'+videoSnapshotURL + '?t='+new Date().getTime()+'" width="80" height="60"/>');
 	     }
 	     
 	     var videoFileURL = record.get("videoFileURL");
 	     if(videoFileURL) {
 	    	Ext.getCmp("previewVideoFile").body.update("视频文件："+videoFileURL);
 	     }
    	 
         view.down('form').loadRecord(record);
    },
    
    actionColumnOptVideoFile: function (column, grid, rowIndex, colIndex, item, e){
        var rec = grid.getStore().getAt(rowIndex);
        if (item.action == 'edit') {  
        	 this.editVideoFile(grid, rec);
        } else if (item.action == 'delete') {  
             this.deleteVideoFile(rec);
        }  
    },
    
    reloadVideoFile:function() {
    	var contentPanel = this.getContentPanel();
    	var videoFileStore = this.getVideoFileStore();
    	contentPanel.removeAll(true);
    	videoFileStore.load();
        contentPanel.add(Ext.widget('VideoFile-Grid'));
        if (contentPanel.floating) {
       	 contentPanel.show();
         }
    },
    
    updateVideoFile: function(button) {
    	var win = button.up('window'),
        form   = win.down('form');
    	var obj = this;
        form.submit({
        	method:'post',
            url: '/videofile/ajaxUpdateVideoFile.action',
            success: function(form, action) {
            	 win.close();
            	 obj.reloadVideoFile();
            },
            failure: function(form, action) {
            	Ext.Msg.alert("提示","更新失败，请重试！");  
            }
        });
    },
    
    deleteVideoFileMore: function(button) {
	    var videoFileGrid = button.up('VideoFile-Grid');
        var data = videoFileGrid.getSelectionModel().getSelection();  
        if(data.length == 0){  
            Ext.Msg.alert("提示","您最少要选择一条数据");  
        }else{  
         	this.deleteVideoFile(data);
        }  
     },
     
     deleteVideoFile: function(data) {
    	 var obj = this;
    	 Ext.MessageBox.confirm("注意", "您真的要做删除操作吗?", function (btn) {
    		 if(btn === 'yes') {
    			 var ids = [];
    	    	 Ext.Array.each(data,function(record){  
    	             ids.push(record.get('uid'));  
    	         });  
    	         Ext.Ajax.request({  
    	             url:'/videofile/ajaxDeleteVideoFile.action',  
    	             params:{uids:ids.join(",")},  
    	             method:'POST',  
    	             timeout:2000,  
    	             success:function(response,opts){
    	            	var rs = Ext.decode(response.responseText);
    	            	if(rs && rs.length>0) {
    	            		Ext.Msg.alert("提示",rs+"视频被使用无法删除!");  
    	            	}
    	             	obj.reloadVideoFile();
    	             }  
    	         });  
    		 }
    	 });
     },
     
    uploadVideoFilePreviewPic:function(file, value, eOpts) {
   	   var form = file.up('form');
         form.submit({
          	clientValidation:false,
              url: 'ajaxUploadFile.action',
              params:{dir:'video'},
              waitMsg: '正在上传文件...',
              success: function(form, action) {
                  var path = action.result.result;
                  Ext.getCmp('videoFileSnapshot').setValue(path);
                  Ext.getCmp('previewVideoFilePreviewPic').body.update('<img src="'+path+'" width="80" height="60"/>');
              },
              failure: function (form, action) {
                  Ext.Msg.alert('出错', action.result.msg);
              }
          });
         file.up('VideoFile-Edit').doLayout();
      },
      
      uploadVideoFile:function(file, value, eOpts) {
    	   var form = file.up('form');
         form.submit({
         	clientValidation:false,
             url: 'ajaxUploadVideoFile.action',
             params:{dir:'video'},
             waitMsg: '正在上传文件...',
             success: function(form, action) {
                 var path = action.result.result;
                 var fileName = action.result.fileName;
                 var fileSize = action.result.fileSize;
                 var mimeType = action.result.mimeType;
                 var fileExt = action.result.fileExt;
                 
                 console.log(path+"---"+fileName+"---"+fileSize+"---"+mimeType+"----"+fileExt);
                 
                 Ext.getCmp('videoPath').setValue(path);
                 Ext.getCmp('videoFileOrigFileName').setValue(fileName);
                 Ext.getCmp('videoFileSize').setValue(fileSize);
                 Ext.getCmp('videoMimeType').setValue(mimeType);
                 Ext.getCmp('videoFileExt').setValue(fileExt);
                 if(fileExt) {
              		   if(fileExt === "avi") {
              			   
              		   } else if(fileExt === "mpg") {
              			   
              		   } else if(fileExt === "rm") {
              			   
              		   } else if(fileExt === "asf" || fileExt === "wmv") {
              			   
              		   } else if(fileExt === "wmv") {
              			   
              		   } else if(fileExt === "wmv") {
              			   
              		   }
              		   
              		   var htmlContent = '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '
  				          +'CODEBASE="data/qtplugin.cab" WIDTH="300" HEIGHT="150" >'
  				          +'<PARAM NAME="src" VALUE="'+path+'" >'
  				          +'<PARAM NAME="autoplay" VALUE="true" >'
  				          +'<EMBED SRC="'+path+'" TYPE="image/x-macpaint" '
  				          +'PLUGINSPAGE="http://www.apple.com/quicktime/download" WIDTH="300" HEIGHT="150" AUTOPLAY="true"></EMBED>'
  				          +'</OBJECT>';
  				       //Ext.getDom("previewVideo").innerHTML = fileName;
              	   }
                
               
                 
                 Ext.getCmp("previewVideoFile").body.update('<div>视频文件上传成功!</div>');
                 
                //var videoEdit = file.up('Video-Edit');
                //videoEdit.doLayout();
                //videoEdit.center();
             },
             failure: function (form, action) {
                 Ext.Msg.alert('出错', action.result.msg);
             }
         });
         file.up('VideoFile-Edit').doLayout();
     }
});